class StringUtil {
  static bool isNullOrEmpty(String? text) {
    return text == null || text.isEmpty;
  }

  static bool isBlank(String text) {
    return text.trim().isEmpty;
  }

  static bool isChinese(String text) {
    final reg = RegExp(chineseRegex);
    return reg.hasMatch(text);
  }

  static String chineseRegex = '[\u4e00-\u9fa5]';

  static String? validator(
      {required String text,
      required ValidateErrorMessage errorMessages,
      required bool isOnlyChinese,
      required bool isEmpty,
      int? maxLength}) {
    if (isEmpty && text.isEmpty) {
      return errorMessages.empty;
    } else if (isOnlyChinese && !isChinese(text)) {
      return errorMessages.language;
    } else if (maxLength != null && text.length > maxLength) {
      return errorMessages.exceeded;
    }
    return null;
  }
}

class ValidateErrorMessage {
  final String empty;
  final String exceeded;
  final String language;

  ValidateErrorMessage({
    this.empty = '',
    this.exceeded = '',
    this.language = '',
  });
}

extension NullableStringExt on String? {
  bool get isNullOrEmpty => this?.isEmpty ?? true;

  bool get isNotNullOrEmpty => this?.isNotEmpty ?? false;
}

extension StringExt on String {
  bool isChinese() {
    return StringUtil.isChinese(this);
  }

  /// 文字长度小于[targetLength]时，用[fillBy]填充首部，[fillBy]只能是长度为1的字符串；
  /// 文字长度大于[targetLength]时，不做处理
  String fillingLength(int targetLength, {String fillBy = '0'}) {
    if (length > targetLength) return this;
    var result = '';
    for (var i = 0; i < targetLength - length; i++) {
      result += fillBy;
    }
    result += this;
    return result;
  }
}
